﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using DownloadBook.Util;

namespace DownloadBook {
    public class Content : IComparable<Content>{
        public static readonly string s_TemplateNumber = "0123456789一二三四五六七八九十百千万亿";
        public static Regex s_RegTitleNumber = new Regex($"第([{s_TemplateNumber}]*?)章");
        public string title;
        public string url;
        public bool downloadComplete;
        public bool downloading;
        private String mFileName;
        public int titleIndex = -1;


        /// <summary>
        /// 获取保存的文件名
        /// </summary>
        /// <returns></returns>
        public String GetFileName(){
            if(mFileName == null){
                string fileName = Path.GetFileName(url);
                char [] invalidChars = Path.GetInvalidFileNameChars();
                for (int i = 0; i < invalidChars.Length; i++) {
                    String cStr = invalidChars[i].ToString();
                    fileName = fileName.Replace(cStr, String.Empty);
                }
                mFileName = fileName;
            }
            return mFileName;
        }

        public bool InitTitleIndex() {
            bool result = false;
            if (this.titleIndex > -1) {
                result = true;
            } else {
                long value = -1;
                Match match = s_RegTitleNumber.Match(this.title);
                if (match.Success && match.Groups.Count > 0) {
                    string str = match.Groups[1].ToString();
                    int index = str.IndexOf("-");
                    if(index > -1) {
                        string strLeft = str.Substring(0, index);
                        if(!long.TryParse(strLeft, out value)) {
                            value = ChineseUtil.ParseCnToInt(strLeft);
                        }
                    }
                    if(value < 0) {
                        if(!long.TryParse(str, out value)) {
                            value = ChineseUtil.ParseCnToInt(str);
                        }
                    }
                }
                if(value < 0) {
                    string strTemp = ExtractNumbers(this.title);
                    if(!long.TryParse(strTemp, out value)) {
                        value = ChineseUtil.ParseCnToInt(strTemp);
                    }
                }
                if(value > -1) {
                    this.titleIndex = (int)value;
                    result = true;
                }
            }
            return result;
        }
        protected string ExtractNumbers(string str) {
            int start = 0;
            StringBuilder sb = new StringBuilder();
            int indexJuan = str.IndexOf("卷");
            if(indexJuan > 0) {
                start = indexJuan + 1;
            }
            for (int i = start; i < str.Length; i++){
                char c = str[i];
                if (s_TemplateNumber.Contains(c)) {
                    sb.Append(c);
                } else {
                    if(sb.Length > 0) {
                        break;
                    }
                }
            }
            return sb.ToString();
        }

        public override string ToString() {
            return title;
        }


        public int CompareTo(Content other) {
            return this.titleIndex.CompareTo(other.titleIndex);
        }
    }
}
